package order

import (
	"context"
	"strings"

	"mall/common/globalkey"
	"mall/common/utils/pointy"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/internal/utils/errorhandler"
	"mall/service/order/rpc/types/order"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateTradeStateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateTradeStateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateTradeStateLogic {
	return &UpdateTradeStateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateTradeStateLogic) UpdateTradeState(in *order.UpdateTradeStateReq) (*order.BaseResp, error) {
	// logrus.Info(fmt.Sprintf("UpdateTradeStateReq: %+v", in))
	// Id:46  TradeState:12

	if (in.Id == nil || *in.Id == 0) && (in.Sn == nil || len(strings.TrimSpace(*in.Sn)) == 0) {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	if in.TradeState == nil || *in.TradeState == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	if in.Sn != nil && len(strings.TrimSpace(*in.Sn)) > 0 {
		filterMap := make(map[string]map[string][]any)
		filterMap[model.OrderColumns.OrderSN] = map[string][]any{
			"equalTo": {strings.TrimSpace(*in.Sn)},
		}
		ret, _, err := l.svcCtx.Order.GetList(l.ctx, model.M{}, -1, filterMap)
		if err != nil {
			// return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			logx.Errorw("BackendGetOrderList GetList statistic err", logx.Field("detail", err.Error()))
		}
		if len(ret) > 0 && ret[0] != nil && ret[0].ID > 0 {
			in.Id = pointy.GetPointer(ret[0].ID)
		}
	}

	trade, err := l.svcCtx.Order.FindOneNoCache(l.ctx, nil, *in.Id)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	status := *in.TradeState
	switch uint32(trade.TradeState) {
	case globalkey.OrderTradeStateNotPay:
		if status == globalkey.OrderTradeStateUserCancelled || status == globalkey.OrderTradeStateAutoCancelled || status == globalkey.OrderTradeStateAdminCancelled {
			trade.TradeState = uint16(status) // 取消订单
			err = l.svcCtx.Order.Update(l.ctx, nil, trade, nil)
			if err != nil {
				return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			}
		}
	case globalkey.OrderTradeStateUserCancelled, globalkey.OrderTradeStateAutoCancelled, globalkey.OrderTradeStateAdminCancelled:
		if status == globalkey.OrderTradeStateUserDeleted || status == globalkey.OrderTradeStateAdminDeleted {
			trade.TradeState = uint16(status) // 删除订单
			err = l.svcCtx.Order.Update(l.ctx, nil, trade, nil)
			if err != nil {
				return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			}
		}
	case globalkey.OrderTradeStatePaid, globalkey.OrderTradeStateShipping:
		if status == globalkey.OrderTradeStateShipped {
			trade.TradeState = uint16(status) // 订单已发货
			err = l.svcCtx.Order.Update(l.ctx, nil, trade, nil)
			if err != nil {
				return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			}
		}
	case globalkey.OrderTradeStateShipped:
		// fmt.Println("触发已收货")
		if status == globalkey.OrderTradeStateConfirmed || status == globalkey.OrderTradeStateAutoConfirmed {
			trade.TradeState = uint16(status) // 订单已收货
			err = l.svcCtx.Order.Update(l.ctx, nil, trade, nil)
			if err != nil {
				return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
			}
		}
	}

	return &order.BaseResp{
		Msg: i18n.UpdateSuccess,
	}, nil
}
